package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.AddressAddReqeust;
import net.xdclass.service.AddressService;
import net.xdclass.until.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import net.xdclass.vo.AddressVO;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2022-01-07
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1/")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("根据id查找地址详情")
    @GetMapping("find/{address_id}")
    public JsonData detail(
            @ApiParam(value = "地址id", required = true)
            @PathVariable("address_id") long id) {
        AddressVO detail = addressService.detail(id);
        return detail == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(detail);
    }

    @ApiOperation("添加用户收货地址")
    @PostMapping("add")
    public JsonData add(@ApiParam("地址对象") AddressAddReqeust addressAddReqeust) {
        addressService.add(addressAddReqeust);
        return JsonData.buildSuccess();
    }

    @ApiOperation("删除用户地址")
    @DeleteMapping("/del/{address_id}")
    public JsonData del(@ApiParam(value = "地址id", required = true)
                        @PathVariable("address_id") int addressId) {
        int rows = addressService.del(addressId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    @ApiOperation("查看用户所有收货地址")
    @GetMapping("/list")
    public JsonData findUserAllAddress() {
        List<AddressVO> list = addressService.listUserAllAddress();
        return JsonData.buildSuccess(list);
    }
}

